package com.oocl.client.ui.impl;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.oocl.client.callback.ChangeFrameCallback;
import com.oocl.client.callback.ProtocolFinishProcessCallback;
import com.oocl.client.process.ProtocolReceiver;
import com.oocl.client.process.ProtocolSender;
import com.oocl.client.ui.BaseFrame;
import com.oocl.client.ui.IChatOperator;
import com.oocl.client.ui.ILogin;
import com.oocl.client.util.Logger;
import com.oocl.qq.protocol.Action;
import com.oocl.qq.protocol.Protocol;

public class LoginFrame extends BaseFrame implements ActionListener, ILogin {
	private JTextField nameTf;
	private JPasswordField psdPf;
	private JButton loginBtn;
	private JButton cancelBtn;
	private JButton registerBtn;

	private String name;

	private LoginFrame mContext;
	
	private ChangeFrameCallback mChangeFrameCallback;
	// /协议发送接口
	private ProtocolSender protocolSender;

	public LoginFrame(){
		this.mContext = this;
	}
	
	public void setProtocolSender(ProtocolSender protocolSender) {
		this.protocolSender = protocolSender;
	}

	public void setChangeFrameCallback(ChangeFrameCallback changeFrameCallback) {
		this.mChangeFrameCallback = changeFrameCallback;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String inputName = this.nameTf.getText();
		char[] inputPsd = this.psdPf.getPassword();
		if(inputName==null|| inputName.length()==0||inputPsd==null || inputPsd.length==0){
			int option = JOptionPane.showConfirmDialog(this, "账户和密码不能为空", "提示 ",
					JOptionPane.OK_OPTION);
			this.psdPf.setText("");
			this.nameTf.setText("");
			return;
		}
		StringBuilder sb= new StringBuilder();
		sb.append(inputPsd);
		this.name = inputName;
		System.out.println("登录用户名： " + inputName + "， 密码： " + sb.toString());
		Protocol p = new Protocol(Action.Login, this.name);
		p.setMsg(sb.toString());
		p.setTime(System.currentTimeMillis());
		sendProtocol(p);
	}

	@Override
	protected void initFrame() {
		this.setSize(400, 260);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("QQ客户端登录");
	}

	@Override
	protected void init() {
		this.setLayout(null);
		nameTf = new JTextField();
		psdPf = new JPasswordField();
		loginBtn = new JButton("login");
		cancelBtn = new JButton("cancel");
		registerBtn = new JButton("Register");
		JLabel namel = new JLabel("您的大名：");
		JLabel psdl = new JLabel("不存在的密码：");
		namel.setBounds(50, 20, 100, 40);
		psdl.setBounds(30, 80, 120, 40);
		nameTf.setBounds(120, 20, 260, 40);
		psdPf.setBounds(120, 80, 260, 40);
		loginBtn.setBounds(40, 140, 90, 40);
		registerBtn.setBounds(160, 140, 90, 40);
		cancelBtn.setBounds(280, 140, 90, 40);

		nameTf.setFont(new Font("宋体", Font.BOLD, 18));

		this.add(namel);
		this.add(psdl);
		this.add(nameTf);
		this.add(psdPf);
		this.add(loginBtn);
		this.add(registerBtn);
		this.add(cancelBtn);
	}

	@Override
	protected void addEvent() {
		loginBtn.addActionListener(this);
		cancelBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});

		registerBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Logger.log("切换到注册界面");
				RegisterFrame registerFrame = new RegisterFrame(mContext);
				registerFrame.setChangeFrameCallback(mChangeFrameCallback);
				registerFrame.setProtocolSender(protocolSender);
				if (mChangeFrameCallback != null)
					mChangeFrameCallback.changeFrame(registerFrame);
			}
		});

	}
	

	@Override
	protected void sendProtocol(Protocol protocol) {
		this.protocolSender.sendProtocol(protocol);
	}

	@Override
	public void loginFail(String msg) {
		
		if(msg==null){
			int option = JOptionPane.showConfirmDialog( 
	                  this, "服务器已断开连接", "提示 ",JOptionPane.OK_OPTION);
			System.exit(0);
		}
		
		///登录失败
		Logger.log("登录失败！"+msg);
		int option = JOptionPane.showConfirmDialog( 
                  this, msg, "提示 ",JOptionPane.OK_OPTION);
		this.psdPf.setText("");
	}

	@Override
	public void finishLogin(String userName,List<String> userList) {
		// /这个时候，表示登录成功了， 应该切换到 ChatFrame
		System.out.println("登录成功！！！现在就新建chatFrame");
		ChatFrame chatFrame = new ChatFrame(userName, userList);
		chatFrame.setProtocolSender(this.protocolSender);
		if (this.mChangeFrameCallback != null)
			this.mChangeFrameCallback.changeFrame(chatFrame);
	}
}
